package trb.complexshield.bool.model;

import trb.complexshield.bool.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility that creates a SegmentsCreator from a Bool object.
 */
public class SegmentsCreator {

    public static SegmentsProducer createSegmentsProducer(Object object) {
        if (object instanceof BoolBox) {
            BoolBox boolBox = (BoolBox) object;
            return new IntersectableSegmentsProducer(new IntersectableBox(boolBox.lower, boolBox.upper, boolBox.l2w));
        } else if (object instanceof BoolCylinder) {
            BoolCylinder boolCylinder = (BoolCylinder) object;
            return new IntersectableSegmentsProducer(new IntersectableCylinder(boolCylinder.radius, boolCylinder.height, boolCylinder.l2w));
        } else if (object instanceof BoolSphere) {
            BoolSphere boolCylinder = (BoolSphere) object;
            return new IntersectableSegmentsProducer(new IntersectableSphere(boolCylinder.radius, boolCylinder.l2w));
        } else if (object instanceof BoolOr) {
            List<SegmentsProducer> producerList = new ArrayList();
            BoolOr boolOr = (BoolOr) object;
            for (Object child : boolOr.children) {
                SegmentsProducer segmentsProducer = createSegmentsProducer(child);
                if (segmentsProducer != null) {
                    producerList.add(segmentsProducer);
                }
            }
            return new OrSegments(producerList);
        } else if (object instanceof BoolSubtract) {
            BoolSubtract boolSubtract = (BoolSubtract) object;
            SegmentsProducer a = createSegmentsProducer(boolSubtract.a);
            SegmentsProducer b = createSegmentsProducer(boolSubtract.b);
            if (a != null && b != null) {
                return new SubtractSegments(a, b);
            }
        }

        return null;
    }
}
